package tq.com.tqstudent.net.ok;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.lib.common.utils.SPUtils;
import com.lib.common.utils.Tips;
import com.lib.common.views.LoadingDialog;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import tq.com.tqandroidim.TQIM;
import tq.com.tqstudent.App;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.UserInfo;

/**
 * @Author rames
 * @date 2019/3/8 15:04
 */
public abstract class DialogCallback<T> extends JsonCallback<BaseBean<T>> {
    private Dialog dialog;
    private Context context;

    private void initDialog(Activity activity) {
        this.context = activity;
        dialog = new LoadingDialog(context);
    }

    public DialogCallback(Activity activity) {
        super();
        this.context = activity;
        initDialog(activity);
    }

    public DialogCallback(Activity activity, boolean isShowDialog) {
        super();
        this.context = activity;
        if(isShowDialog) {
            initDialog(activity);
        }
    }

    @Override
    public void onStart(Request<BaseBean<T>, ? extends Request> request) {
        super.onStart(request);
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    @Override
    public void onFinish() {
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onError(Response<BaseBean<T>> response) {
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }

        handleFail(response.body());
    }

    @Override
    public void onSuccess(Response<BaseBean<T>> response) {
        if(response.body().getRet() == 0){
            if(!UserInfo.isLogin() && !TextUtils.isEmpty(response.body().getTk())) {
                SPUtils.getInstance(context).putString(SPUtils.TOKEN, response.body().getTk());
            }
            handleSuccess(response.body().getData());
        }else{
            handleFail(response.body());
        }
    }

    public abstract void handleSuccess(T data);

    public void handleFail(BaseBean body){
        if(null == this.context){
            context = App.getApp();
        }

        if(null == body) {
            Tips.showTips(context,"请求失败");
            return;
        }

        if(!TextUtils.isEmpty(body.getMsg())){
            Tips.showTips(context, body.getMsg());
            return;
        }

        switch (body.getRet()) {
            case 10000:
                Tips.showTips(context,"系统错误");
                break;

            case 10010:
                Tips.showTips(context,"无效请求");
                break;

            case 10011:
                UserInfo.loginOut();
                Tips.showTips(context,"身份过期或者无效");
                break;

            default:
                Tips.showTips(context,"请求失败");
                break;
        }
    }

    @Override
    public BaseBean<T> convertResponse(okhttp3.Response response) throws Throwable {
        ResponseBody body = response.body();
        if(null == body) return null;

        BaseBean<T> data = null;
        Gson gson = new Gson();
        JsonReader jsonReader = new JsonReader(body.charStream());

        Type genType = getClass().getGenericSuperclass();
        Type type = ((ParameterizedType)genType).getActualTypeArguments()[0];

        data = gson.fromJson(jsonReader, new ParameterizedType() {
            @Override
            public Type[] getActualTypeArguments() {
                return new Type[]{type};
            }

            @Override
            public Type getRawType() {
                return BaseBean.class;
            }

            @Override
            public Type getOwnerType() {
                return null;
            }
        });

        return data;
    }
}
